<template>
    <div class="wrapper">
        <div style="height: 60px; line-height: 60px; font-size: 20px; padding-left: 50px; color: white;
      background-color: rgba(0,0,0,0.2)">
            <img src="../../src/assets/imgs/中跆协-1.jpg" alt="" style="width: 232px;
       position: relative; top: -1px; right:50px"><!--background-color: #1f2d3d;-->
            <!--      中跆协积分排名系统-->
        </div>
        <div style="margin: 150px auto; background-color: #fff; width: 350px; height: 300px; padding: 20px; border-radius: 10px">
            <div style="margin: 20px 0; text-align: center; font-size: 24px"><b>登 录</b></div>
            <el-form :model="user" :rules="rules" ref="userForm">
                <el-form-item prop="userAccount">
                    <el-input size="medium" prefix-icon="el-icon-user" v-model="user.userAccount"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input size="medium" prefix-icon="el-icon-lock" show-password v-model="user.password"></el-input>
                </el-form-item>

                <el-form-item style="margin: 10px 0; text-align: right">
                    <el-button type="primary" size="small" autocomplete="off" @click="login">登录</el-button>
                </el-form-item>

            </el-form>
        </div>
    </div>
</template>

<script>
    import {resetRouter, setRoutes} from "@/router";
    import Identify from "@/components/Identify";

    export default {
        name: "Login",
        data() {
            return {
                user: {},
                pass: {},
                code: '',
                dialogFormVisible: false,
                // 图片验证码
                identifyCode: '',
                // 验证码规则
                identifyCodes: '3456789ABCDEFGHGKMNPQRSTUVWXY',
                rules: {
                    userAccount: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur'}
                    ],
                }
            }
        },
        components: {Identify},
        mounted() {
            // 重置路由
            resetRouter()
            this.refreshCode()
        },
        methods: {
            login() {
                // if (this.code !== this.identifyCode.toLowerCase()) {
                //   this.$message({
                //     type: "error",
                //     message: "验证码错误"
                //   })
                //   return;
                // }
                this.$refs['userForm'].validate((valid) => {
                    if (valid) {  // 表单校验合法
                        this.request.post("/user/login", this.user).then(res => {
                            if (res.code === 200) {
                                localStorage.setItem("user", JSON.stringify(res.data))  // 存储用户信息到浏览器
                                localStorage.setItem("menus", JSON.stringify(res.data.menus))  // 存储用户信息到浏览器

                                // 动态设置当前用户的路由
                                setRoutes()
                                //导向第一个菜单
                                if(res.data.menus[0].path === null || res.data.menus[0].path===""){
                                    this.$router.push(res.data.menus[0].children[0].path)
                                }else {
                                    this.$router.push(res.data.menus[0].path)
                                }

                                this.$message.success("登录成功")
                            } else {
                                this.$message.error(res.message)
                            }
                        })
                    }
                });
            },
            // 切换验证码
            refreshCode() {
                this.identifyCode = ''
                this.makeCode(this.identifyCodes, 4)
            },
            // 生成随机验证码
            makeCode(o, l) {
                for (let i = 0; i < l; i++) {
                    this.identifyCode += this.identifyCodes[Math.floor(Math.random() * (this.identifyCodes.length))]
                }
            },
            handlePass() {
                this.dialogFormVisible = true
                this.pass = {}
            },
            passwordBack() {
                this.request.put("/user/reset", this.pass).then(res => {
                    if (res.code === 200) {
                        this.$message.success("重置密码成功，新密码为：123，请尽快修改密码")
                        this.dialogFormVisible = false
                    } else {
                        this.$message.error(res.message)
                    }
                })
            }
        }
    }
</script>

<style>
    .wrapper {
        height: 100vh;
        background-image: linear-gradient(to bottom right, #4169E1, #87CEFA);
        overflow: hidden;
    }
</style>
